﻿/*

    This file is a part of BinaryDialog.
 
    BiLog IM aka BinaryDialog - WPF Client for XMPP Protocol
    Copyright (C) 2008  Christophe LEMOINE

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Security;
using System.Security.Cryptography;

namespace BinaryDialog.Datas
{

    [Serializable]
    public class Account : INotifyPropertyChanged
    {

        private string m_NickName = string.Empty;
        [XmlElement(ElementName = "NickName")]
        public string NickName
        {
            get { return this.m_NickName; }
            set
            {
                if (this.m_NickName == value.Trim()) 
                {
                    return; 
                }
                this.m_NickName = value.Trim();
                if(this.m_ScreenName.Length == 0)
                {
                    this.ScreenName = this.m_NickName;
                }
                this.OnPropertyChanged(new PropertyChangedEventArgs("NickName"));
            }
        }
        
        private string m_ScreenName = string.Empty;
        [XmlElement(ElementName = "ScreenName")]
        public string ScreenName
        {
            get { return this.m_ScreenName; }
            set 
            {
                if (this.m_ScreenName == value.Trim())
                {
                    return;
                }
                this.m_ScreenName = value.Trim();
                this.OnPropertyChanged(new PropertyChangedEventArgs("ScreenName"));
            }
        }

        private string m_Server = "jabber.fr";
        [XmlElement(ElementName = "Server")]
        public string Server
        {
            get { return this.m_Server; }
            set
            {
                if (this.m_Server == value.Trim())
                {
                    return;
                }
                this.m_Server = value.Trim();
                this.OnPropertyChanged(new PropertyChangedEventArgs("Server"));
            }
        }

        private Int32 m_Port = 5222;
        [XmlElement(ElementName = "Port")]
        public Int32 Port
        {
            get { return this.m_Port; }
            set
            {
                if (value >= 0 && value <= 65535)
                {
                    if (this.m_Port == value)
                    {
                        return;
                    }
                    this.m_Port = value;
                    this.OnPropertyChanged(new PropertyChangedEventArgs("Port"));
                }
            }
        }

        private bool m_UseSSL = false;
        [XmlElement(ElementName = "UseSSL")]
        public bool UseSSL
        {
            get { return this.m_UseSSL; }
            set
            {
                if (this.m_UseSSL == value)
                {
                    return;
                }
                if (value)
                {
                    this.UseStartTLS = false;
                }
                this.m_UseSSL = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("UseSSL"));
            }
        }

        private bool m_UseStartTLS = true;
        [XmlElement(ElementName = "UseStartTLS")]
        public bool UseStartTLS
        {
            get { return this.m_UseStartTLS; }
            set
            {
                if (this.m_UseStartTLS == value)
                {
                    return;
                }
                if (value)
                {
                    this.UseSSL = false;
                }
                this.m_UseStartTLS = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("UseStartTLS"));
            }
        }

        private byte[] m_Avatar = null;
        [XmlElement(ElementName = "Avatar")]
        public byte[] Avatar
        {
            get { return this.m_Avatar; }
            set
            {
                if (this.m_Avatar == value)
                {
                    return;
                }
                this.m_Avatar = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Avatar"));
            }
        }

        private string m_Password = string.Empty;
        [XmlElement(ElementName = "Password")]
        public string Password
        {
            get
            {
                return this.m_Password;
            }
            set
            {
                if (this.m_Password == value)
                {
                    return;
                }
                this.m_Password = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Password"));
            }
        }

        private bool m_RegisterDemand = false;
        [XmlIgnore]
        public bool RegisterDemand
        {
            get { return this.m_RegisterDemand; }
            set
            {
                if (this.m_RegisterDemand == value)
                {
                    return;
                }
                this.m_RegisterDemand = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("RegisterDemand"));
            }
        }

        private bool m_Connected = false;
        [XmlIgnore]
        public bool Connected
        {
            get { return this.m_Connected; }
            set
            {
                if (this.m_Connected == value)
                {
                    return;
                }
                this.m_Connected = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Connected"));
            }
        }

        [OnSerializing]
        void BeforeSerialize(StreamingContext context)
        {
            if (this.ScreenName.Length == 0) { this.ScreenName = this.NickName; }
            if (this.Server.Length == 0) { this.Server = "jabber.fr"; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }

        public Account()
        { }

        public Account(string NickName, string Password, string Server, Int32 Port, bool UseSSL, bool UseStartTLS)
        {
            this.NickName = NickName;
            this.Password = Password;
            this.Server = Server;
            this.Port = Port;
            this.UseSSL = UseSSL;
            this.UseStartTLS = UseStartTLS;
        }

        public override string ToString()
        {
            return this.NickName + "@" + this.Server + ":" + this.Port.ToString();
        }

    }

}
